package cn.com.bsfit.netty.config.properties;

import cn.com.bsfit.netty.config.base.BaseConfigurable;
import cn.com.bsfit.netty.config.constants.NettyConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * 类名 BaseNettyServerProperties
 * 描述 Netty 服务器部分配置项的可配置化,可以自己继承并且重写该配置
 *
 * @author hedonglin
 * @version 1.0
 * @date 2019/9/29 10:01
 */
@Component
@ConfigurationProperties(value = "netty.server")
@Data
public class BaseNettyServerProperties implements BaseConfigurable, Serializable {
    private static final long serialVersionUID = -3348850281802288774L;

    /**
     * netty 服务器端口
     */
    private int port = 9999;
    /**
     * 是否启动Netty
     */
    private boolean enable = false;

    /**
     * 主线程池大小
     */
    private int bosses = 1;

    /**
     * 工作线程池大小
     */
    private int workers = 1;

    /**
     * 数据转换线程池大小
     */
    private int dataTransPollSize = 2;

    /**
     * Netty服务器启动延时时间,默认1000毫秒
     */
    private int startDelayTime = 1000;

    /**
     * 支持的顶层字段列表
     */
    private String topFields = NettyConstants.REQ_TOP_DEFAULT_KEY;
    /**
     * 支持的Head字段列表
     */
    private String headFields = NettyConstants.REQ_HEAD_DEFAULT_KEY;
    /**
     * 支持的Body字段列表
     */
    private String bodyFields = NettyConstants.REQ_BODY_DEFAULT_KEY;


    /**
     * 接受数据的Buffer大小
     */
    private RecBuf recbuf = new RecBuf();
    /**
     * 回写数据配置
     */
    private WriteBuf writeBuf = new WriteBuf();

    /**
     * JSON响应参数配置
     */
    private JsonResponse jsonResponse = new JsonResponse();

    /**
     * Xml响应参数配置
     */
    private XmlResponse xmlResponse = new XmlResponse();

    /**
     * 读取缓冲区配置
     *
     * @author Administrator
     */
    @Data
    public class RecBuf {
        /**
         * receive data timeout, default 30s
         */
        private long timeout = 30;
        /**
         * receiver buffer min size,default 1K
         */
        private int minSize = 1024;

        /**
         * receiver buffer initial size,default 10K
         */
        private int initial = 10240;

        /**
         * receiver buffer max size,default 1M
         */
        private int maxSize = 1048576;
    }


    /**
     * 写入缓冲区配置
     */
    public class WriteBuf implements Serializable{
        /**
         * write data timeout, default 30s
         */
        private long timeout = 30;

        public long getTimeout() {
            return timeout;
        }

        public void setTimeout(long timeout) {
            this.timeout = timeout;
        }
    }

    /**
     * TCP+JSON响应配置
     */
    public class JsonResponse implements Serializable{
        private static final long serialVersionUID = 1692293159004784887L;
        /**
         * 是否包含顶层Message
         */
        private boolean isContainsTop = false;
        /**
         * 是否包含Head
         */
        private boolean isContainsHead = true;
        /**
         * 是否包含Body
         */
        private boolean isContainsBody = true;

        public boolean isContainsTop() {
            return isContainsTop;
        }

        public void setContainsTop(boolean containsTop) {
            isContainsTop = containsTop;
        }

        public boolean isContainsHead() {
            return isContainsHead;
        }

        public void setContainsHead(boolean containsHead) {
            isContainsHead = containsHead;
        }

        public boolean isContainsBody() {
            return isContainsBody;
        }

        public void setContainsBody(boolean containsBody) {
            isContainsBody = containsBody;
        }
    }


    /**
     * TCP+XML响应配置
     */
    public class XmlResponse implements Serializable{
        private static final long serialVersionUID = -5623980550925505853L;
        /**
         * 是否包含顶层Message
         */
        private boolean isContainsTop = true;
        /**
         * 是否包含Head
         */
        private boolean isContainsHead = true;
        /**
         * 是否包含Body
         */
        private boolean isContainsBody = true;

        public boolean isContainsTop() {
            return isContainsTop;
        }

        public void setContainsTop(boolean containsTop) {
            isContainsTop = containsTop;
        }

        public boolean isContainsHead() {
            return isContainsHead;
        }

        public void setContainsHead(boolean containsHead) {
            isContainsHead = containsHead;
        }

        public boolean isContainsBody() {
            return isContainsBody;
        }

        public void setContainsBody(boolean containsBody) {
            isContainsBody = containsBody;
        }
    }
}
